package com.zz.framework.core.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter4.BaseQuickAdapter
import kotlin.properties.Delegates

/**
 * 封装一个kotlin下的通用adapter
 * KotlinAdapter.Builder<String, ItemPodcastBinding>(ItemPodcastBinding::class.java).addBindView { holder, item ->}.create()
 */
class CommonAdapter<T : Any, VB : ViewBinding> private constructor(val bindingClass: Class<VB>) : BaseQuickAdapter<T, ViewBindingHolder<VB>>() {

    //绑定事件的lambda放发
    private var addBindView: ((ViewBindingHolder<VB>, T?, List<Any>) -> Unit)? = null

    //view被点击
    var itemClick: ((ViewBindingHolder<VB>, T?) -> Unit)? = null
    var itemLongClick: ((ViewBindingHolder<VB>, T?) -> Unit)? = null


    override fun onBindViewHolder(holder: ViewBindingHolder<VB>, position: Int, item: T?) {
        convert(holder, item, emptyList())
    }

    override fun onBindViewHolder(holder: ViewBindingHolder<VB>, position: Int, item: T?, payloads: List<Any>) {
        convert(holder, item, payloads)
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): ViewBindingHolder<VB> {
        val inflater = LayoutInflater.from(parent.context)
        val viewBinding = createViewBinding(bindingClass = bindingClass, inflater, parent)
        return ViewBindingHolder(viewBinding!!)
    }

    private fun <VB : ViewBinding> createViewBinding(bindingClass: Class<VB>?, inflater: LayoutInflater, parent: ViewGroup?): VB? {
        val method = bindingClass?.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java)
        return method?.invoke(null, inflater, parent, false) as? VB
    }

    fun convert(holder: ViewBindingHolder<VB>, item: T?, payloads: List<Any>) {
        addBindView?.invoke(holder, item, payloads)
        holder.binding.root.setOnClickListener {
            itemClick?.invoke(holder, item)
        }
        itemLongClick?.let {
            holder.binding.root.setOnLongClickListener { _ ->
                it.invoke(holder, item)
                true
            }
        }
    }


    /**
     * 设置数据
     */
    fun setNewData(lists: List<T>?) {
        submitList(lists?.toMutableList())
    }

    /**
     * 建造者，用来完成adapter的数据组合
     */
    class Builder<T : Any, VB : ViewBinding>(val bindingClass: Class<VB>) {

        private var adapter: CommonAdapter<T, VB> by Delegates.notNull()

        init {
            adapter = CommonAdapter(bindingClass)
        }

        /**
         * 设置数据
         */
        fun setNewData(lists: List<T>?): Builder<T, VB> {
            adapter.submitList(lists?.toMutableList())
            return this
        }

        /**
         * 绑定View和数据
         */
        fun addBindView(itemBind: (holder: ViewBindingHolder<VB>, item: T?, payloads: List<Any>) -> Unit): Builder<T, VB> {
            adapter.addBindView = itemBind
            return this
        }

        /**
         * item点击回调
         */
        fun onItemClick(itemClick: (holder: ViewBindingHolder<VB>, item: T?) -> Unit): Builder<T, VB> {
            adapter.itemClick = itemClick
            return this
        }

        fun onItemLongClick(itemClick: (holder: ViewBindingHolder<VB>, item: T?) -> Unit): Builder<T, VB> {
            adapter.itemLongClick = itemClick
            return this
        }

        fun create(): CommonAdapter<T, VB> {
            return adapter
        }

    }
}